package com.framework.modules.base.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.Query;

import com.framework.modules.base.dao.PostsAttrDao;
import com.framework.modules.base.entity.PostsAttrEntity;
import com.framework.modules.base.service.PostsAttrService;


@Service("postsAttrService")
public class PostsAttrServiceImpl extends ServiceImpl<PostsAttrDao, PostsAttrEntity> implements PostsAttrService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<PostsAttrEntity> page = this.selectPage(
                new Query<PostsAttrEntity>(params).getPage(),
                new EntityWrapper<PostsAttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Integer addlikeUids(Integer postId, String likeUid) {
        PostsAttrEntity postsAttrEntity=this.baseMapper.selectByPostId(postId);
        if(postsAttrEntity==null){
            postsAttrEntity=new PostsAttrEntity();
            postsAttrEntity.setPostId(postId);
            postsAttrEntity.setLikeUids(likeUid.toString());
            return baseMapper.insert(postsAttrEntity);
        }
        String strLikeUids=postsAttrEntity.getLikeUids();
        if(StringUtils.isBlank(strLikeUids)){
            strLikeUids=likeUid;
        }else{
            if(Arrays.asList(strLikeUids.split(",")).contains(likeUid)){
                return 0;
            }else{
                strLikeUids=strLikeUids+","+likeUid;
            }
        }
        postsAttrEntity.setLikeUids(strLikeUids);
        return baseMapper.updateAllColumnById(postsAttrEntity);
    }

    @Override
    public Integer reducelikeUids(Integer postId, String likeUid) {
        PostsAttrEntity postsAttrEntity=this.baseMapper.selectByPostId(postId);
        if(postsAttrEntity==null){
            postsAttrEntity=new PostsAttrEntity();
            postsAttrEntity.setPostId(postId);
            postsAttrEntity.setLikeUids("");
            return baseMapper.insert(postsAttrEntity);
        }
        String strLikeUids=postsAttrEntity.getLikeUids();
        if(StringUtils.isBlank(strLikeUids)){
            strLikeUids=likeUid;
        }else{
            List<String> likeUidList= Arrays.asList(strLikeUids.split(","));
            if(likeUidList.contains(likeUid)){
                likeUidList=likeUidList.stream().filter(list->!list.equals(likeUid)).collect(Collectors.toList());
                strLikeUids=StringUtils.join(likeUidList,",");
            }else{
                return 0;
            }
        }
        postsAttrEntity.setLikeUids(strLikeUids);
        return baseMapper.updateAllColumnById(postsAttrEntity);
    }

}
